
package com.kanjian.music.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.adapter.SimpleFMListAdapter;
import com.kanjian.music.entity.FM;
import com.kanjian.music.entity.Music;
import com.kanjian.music.entity.User;
import com.kanjian.music.network.HttpLoader;
import com.kanjian.music.util.PlayerUtil;

/**
 * 电台播放器内电台列表页
 * 
 * @author xuyanjun
 */
public class MusicPlayerRadioListFragemnt extends BaseFragment implements LoaderCallbacks<String> {

    public static MusicPlayerRadioListFragemnt newInstance() {
        return new MusicPlayerRadioListFragemnt();
    }

    private View mRootView;

    private SimpleFMListAdapter mListAdapter;

    ArrayList<FM> mFMList = new ArrayList<FM>(8);

    private ListView mListView;
    
    public int mCurrentRadioType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initFMList();
        mListAdapter = new SimpleFMListAdapter((BaseActivity) getActivity(), mFMList, this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        return mRootView;
    }

    private void initFMList() {
        FM fm = new FM();
        if (User.isLogin()) {
            fm.mFMId = 7;
            fm.mName = "我的电台";
            mFMList.add(fm);
        } else {
            fm = new FM();
            fm.mFMId = 0;
            fm.mName = "精选电台";
            mFMList.add(fm);
        }
        fm = new FM();
        fm.mFMId = 1;
        fm.mName = "流行电台";
        mFMList.add(fm);
        fm = new FM();
        fm.mFMId = 3;
        fm.mName = "摇滚电台";
        mFMList.add(fm);
        fm = new FM();
        fm.mFMId = 2;
        fm.mName = "民谣电台";
        mFMList.add(fm);
        fm = new FM();
        fm.mFMId = 5;
        fm.mName = "嘻哈电台";
        mFMList.add(fm);
        fm = new FM();
        fm.mFMId = 4;
        fm.mName = "电子电台";
        mFMList.add(fm);
        fm = new FM();
        fm.mFMId = 6;
        fm.mName = "其他电台";
        mFMList.add(fm);
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_fm, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.fm_listview);
        mListAdapter.registerOnItemClickListener(mListView);
        mListView.setAdapter(mListAdapter);
    }

    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            if (musicList != null) {
                PlayerUtil.mCurrentRadioType = mCurrentRadioType;
                PlayerUtil.addTempleMusicListThenPlay(musicList,
                        ((BaseActivity) getActivity()).mPlayService);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    };
}
